$(function () {

    listDemand(1);
    if(!isOpen){
        var context = '<div class="text-center pt10 mt20 pl20 pr20">' +
            '<p><span class="fs16">平台检查到您还未开通268云视频服务</br>是否要开通?</span></p>'+
            '</div>';
        parent.layer.confirm(
            context,
            {
                type: 1,
                title:'友情提示：',
                area: ['380px'],
                offset: 'auto',
                closeBtn:0,
                btn:['去开通'],
                btnAlign : 'c'
            },
            function (index) {
                parent.layer.close(index);
                getRedirectMenu(baseloaction+"/admin/course/serviceConfig",false)
            });
    }
    $('body').on('click', '#btn1, #btn2', function() {
        listDemand(1);
    }).on('click', '#demand-list-context i.fa-trash', function () {
        var parent = $(this).parents('div:eq(0)');
        icModale(2, '确认删除选中录播吗?', '', function () {
            deleteDemand(parent, parent.data('varchar'));
        })
    }).on('click', '#demand-list-context i.fa-eye', function () {
        var parent = $(this).parents('div:eq(0)');
        previewDemand(parent.data('name'), parent.data('varchar'));
    }).on('click','#batchUpload',function () {//批量上传
        window.location.href = baseloaction +'/admin/course/batchUpload';
    });

    // 删除录播
    function deleteDemand(obj, id_varchar) {
        $.ajax({
            url: baseloaction + '/admin/course/ajaxDemandDelete',
            data: {'id_varchar': id_varchar},
            dataType: 'json',
            type: 'post',
            success: function (response) {
                if (response.success) {
                    obj.parent('div').parent('div').parent('div').remove();
                    icModale(0, response.message);
                } else {
                    icModale(0, response.message);
                }
            },
            error: function () {
                icModale(0, '系统繁忙，请稍候再试');
            }
        })
    }

    // 预览录播
    function previewDemand(name, id_varchar) {
        parent.layer.open({
            type: 1,
            title: '<strong>' + name + '</strong><small class="ml10"></small>',
            shadeClose: true,
            shade: [0.3, '#000000'],
            area: ['1000px', '520px'],
            content: '<script src="https://v.96koo.net/static/video/js/creatVideoPlayer.js?width=99%&height=99%&videoId=' + id_varchar + '"></script>'
        });
    }

    function listDemand(currentPage) {
        var params = $("#search-form").serializeJSON();
        params.isOpen =isOpen;
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/course/ajaxDemandList',//请求URL
            contextId: 'demand-list-context',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: currentPage,//页码
            pageCall: listDemand, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'all-course', //全选框ID
            callback: function () {//数据加载完成回调
                $("#count").text('（共'+total+'个录播）');
            }
        };
        ajaxGetListData(obj);
    }
});